using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Impact.Infrastructure.Common
{

    /// <summary>
    /// simplified accessor for TLS 
    /// </summary>
    public static class ThreadLocalStorage
    {

        /// <summary>
        /// retrieve an object associated w/ the current thread by key
        /// </summary>
        /// <typeparam name="T">the type of the object to retrieve</typeparam>
        public static T GetThreadObject<T>(string key)
        {
            LocalDataStoreSlot slot = Thread.GetNamedDataSlot(key);
            return (T)Thread.GetData(slot);
        }

        /// <summary>
        /// store an object in the current thread storage area with the given key
        /// </summary>
        public static void SetThreadObject(string key, object value)
        {
            LocalDataStoreSlot slot = Thread.GetNamedDataSlot(key);
            if (slot == null)
            {
                slot = Thread.AllocateNamedDataSlot(key);
            }

            Thread.SetData(slot, value);
        }

    }
}
